package cn.tedu._07springaop.egaop02;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CartServiceAspect {

    /**
     * 前置通知
     */
//    @Before(value = "execution(public boolean cn.tedu._07springaop.egaop02.CartServiceImpl.*(..))")
//    public void before(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println("前置通知：" + name + "方法开始执行");
//    }

    /**
     * 后置通知
     */
//    @After(value = "execution(public boolean cn.tedu._07springaop.egaop02.CartServiceImpl.*(..))")
//    public void after(JoinPoint joinPoint){
//        String name = joinPoint.getSignature().getName();
//        System.out.println("后置通知：" + name + "方法执行结束");
//    }

    /**
     * 返回通知
     */
//    @AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop02.CartServiceImpl.*(..))",
//    returning = "result")
//    public void afterReturning(JoinPoint joinPoint,Object result){
//        String name = joinPoint.getSignature().getName();
//        System.out.println("返回通知："+name+"方法执行完毕，结果为："+result);
//    }

    /**
     * 环绕通知
     */
    @Around(value = "execution(public boolean cn.tedu._07springaop.egaop02.CartServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result = null;
        String name = joinPoint.getSignature().getName();
        try {
            System.out.println("前置通知：" + name + "方法开始执行");
            result = joinPoint.proceed();
            System.out.println("返回通知："+name+"方法执行完毕，结果为："+result);
        } catch (Throwable throwable){
            System.out.println("异常通知："+name+"方法抛出了异常，提示信息："+throwable.getMessage());
        } finally {
            System.out.println("后置通知：" + name + "方法执行结束");
        }
        return result;
    }
}
